//
//  IGTLoginManager.m
//  jingdongfang
//
//  Created by BOE on 2017/9/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTLoginManager.h"
#import <UMSocialCore/UMSocialCore.h>

typedef void(^LoginResultBlock)(id resp);
@interface IGTLoginManager()

@property (nonatomic, strong) TencentOAuth *tencentOAuth;
@property (nonatomic, strong) IGLoginApi *loginApi;
@property (nonatomic, assign) IGLoginType loginType;

@property (nonatomic, copy) LoginResultBlock loginResult;

@property (nonatomic, strong) UIViewController *vc;

@end

@implementation IGTLoginManager

+ (IGTLoginManager *)shareManager {
    static IGTLoginManager *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[IGTLoginManager alloc] init];
    });
    return manager;
}

- (void)loginWithType:(IGLoginType)type vc:(UIViewController *)vc block:(nullable void(^)(id Resp))block {
    self.loginType = type;
    self.loginResult = block;
    self.vc = vc;
    
    self.loginApi = [[IGLoginApi alloc] init];
    self.loginApi.loginType = self.loginType;
    self.loginApi.clientType = IGTClientTypeapp;
    
    if (type == IGLoginTypeWChat) {
        [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
        
    } else if(type == IGLoginTypeQQ) {
        [self getUserInfoForPlatform:UMSocialPlatformType_QQ];

    } else if(type == IGLoginTypeWeiBo) {
        [self getUserInfoForPlatform:UMSocialPlatformType_Sina];
        
    }
}

#pragma mark - 登录 获取用户信息
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    __weak typeof(self) weakSelf = self;
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self.vc completion:^(id result, NSError *error) {
        if (error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentMessageTips:NSLocalizedString(@"We cannot retrieve 3rd-party information. Please try again!", nil)];
            });
        } else {
            UMSocialUserInfoResponse *resp = result;
            weakSelf.loginApi.accessToken = resp.accessToken;
            if (platformType == UMSocialPlatformType_Sina) {
                weakSelf.loginApi.loginPlat = IGTloginPlatTypeweibo;
                weakSelf.loginApi.openId = resp.usid;
            } else if (platformType == UMSocialPlatformType_WechatSession) {
                weakSelf.loginApi.loginPlat = IGTloginPlatTypewx;
                weakSelf.loginApi.openId = resp.unionId;
            } else if (platformType == UMSocialPlatformType_QQ) {
                weakSelf.loginApi.loginPlat = IGTloginPlatTypeqq;
                weakSelf.loginApi.openId = resp.usid;
            }
            weakSelf.loginApi.nike = resp.name;
            weakSelf.loginApi.image = resp.iconurl;
            
            if (weakSelf.loginApi.openId == nil) {
                return;
            }
            [weakSelf requestLogin];
        }
    }];
}

#pragma mark -登录请求
- (void)requestLogin {
    __weak typeof(self) weakSelf = self;
    [self.loginApi startWithDecodeClass:[IGLoginResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            if (weakSelf.loginResult) {
                weakSelf.loginResult(responseObject);
            }
        }
    }];
}

@end
